import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
declare var echarts: any;
@Component({
	selector: 'sa-car-online',
	templateUrl: './car-online.component.html',
	styleUrls: ['./car-online.component.css']
})
export class CarOnlineComponent implements OnInit {
	// 查询参数
	selectedCompanyId = ''; //过滤条件-公司ID
	selectedCompanyName = ''; //过滤条件-公司名称
	selectedCompanyCode = '';//过滤条件-公司code
	dateType = '';		//过滤条件-时间类型，默认按天汇总
	// 分页表单数据
	tableData: any = []; // table的表单数据
	totalCounts: number = 0; //分页总条数
	curPage: number = 1; //分页当前页
	pageSize: number = 10; //内置的分页每页条数
	// 升降序
	sortFlag: any = 2; // 升降序的标志
	onlineCarSort: any = "";// 在线车辆的
	offlineCarSort: any = "";// 离线车联的
	onlineRate: any = ""; // 在线率

	// 全部的表单数据
	allData: any = []; // 

	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute) {
		activatedRoute.queryParams.subscribe(queryParams => {
		});
	}

	ngOnInit() {
		this.load();
	}
	//刷新
	table_search() {
		this.curPage = 1;
		this.load();
	}
	//table刷新
	refresh() {
		this.load();
	}
	//分页回调
	paginate(e) {
		this.curPage = e.page + 1;
		this.pageSize = e.rows;
		// this.load();
		this.tableData = this.allData.slice(this.pageSize * (this.curPage - 1), this.pageSize * this.curPage)
	}
	//初始化加载list数据
	load() {
		let sortByItem: any = "";
		let sortTypeItem: any = "";
		if (this.sortFlag) {
			sortByItem = (Math.ceil(this.sortFlag / 2) - 1) + "";
			sortTypeItem = ((this.sortFlag % 2 == 0) ? 1 : 0) + ""

		}
		let param = {
			"pageSize": this.pageSize,
			"nowPage": this.curPage,
			"parameters": {
				companyId: this.selectedCompanyId,
				type: this.dateType,
				sortBy: sortByItem,
				sortType: sortTypeItem
			}
		}
		let reqdata = 'gridPager=' + JSON.stringify(param);
		this.jsonApiService.http("environmentalCarStatis/onLineStatis.html", reqdata).subscribe(result => {
			let data = []
			if (result.success) {
				data = result.data
				this.allData = result.data;
				this.tableData = result.data.slice(this.pageSize * (this.curPage - 1), this.pageSize * this.curPage)
				this.totalCounts = result.data.length;
			}
		});
	}
	//公司选中回调
	companySelected(data) {
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
		this.selectedCompanyCode = data.companyCode;
	}
	columnSort(num) {
		this.sortFlag = num;
		this.load()
	}
	// 跳转离线明细
	goOfflineDetail(row) {
		// let BaseURL = window.location.protocol + '//' + window.location.hostname + ':' + window.location.port;
		row.dateType = this.dateType
		common.setCookie('offlineDetail', JSON.stringify(row));
		this.router.navigate(['/environment-manage/car-offline-detail']);
		// 打开新的标签页
		// window.open(BaseURL+'#/environment-manage/car-offline-detail');
	}
	// 数据导出
	exportData() {
		let sortByItem: any = "";
		let sortTypeItem: any = "";
		if (this.sortFlag) {
			sortByItem = (Math.ceil(this.sortFlag / 2) - 1) + "";
			sortTypeItem = ((this.sortFlag % 2 == 0) ? 1 : 0) + ""
		}
		// 导出的
		var parameters = {
			companyId: this.selectedCompanyId,
			type: this.dateType,
			sortBy: sortByItem,
			sortType: sortTypeItem
		};
		// let timeCheck = common.correctTime(parameters, ['sortBy', 'sortType'])
		// if(!timeCheck.status){
		// 	this.jsonApiService.message(timeCheck.message);
		// 	return;
		// }
		common.exprotDataFile(parameters, 'param', config.BaseURL + 'environmentalCarStatis/exportOnLineStatis.html')	
	}
}